




import 'dart:io';
import 'dart:isolate';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ip_check_demo/utils/GetInputTextFieldUtils.dart';
import 'package:ip_check_demo/utils/hint_uitl.dart';

class ArpPage extends StatefulWidget{

  const ArpPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return ArpState();
  }
}


class ArpState extends State<ArpPage>{
  String Ip = '';
  TextEditingController _IpinpuController = TextEditingController();
  String text= '';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:
      AppBar(
        title: Text("Ping"),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          getStartIpInput()!,
          ElevatedButton(
            child: Text("Ping一下"),
            onPressed: () {
              PingFunc();
            },

          ),

          Container(
            padding: EdgeInsets.all(8),
            child:
            Text(text),
          )
        ],
      ),
    );
  }


  PingFunc() async {
    print(Ip);
    HintUtil.toast("正在执行操作...");
    text =  "";

    if(mounted){
      setState(() {
      });
    }

    ReceivePort port = ReceivePort();
    Isolate iso = await Isolate.spawn(checkConn,[port.sendPort,Ip]);
    String result = await port.first as String;
    text =  result.toString();

    port.close();
    iso.kill();

    if(mounted){
      setState(() {
      });
    }
  }

  static Future<void> checkConn(List<dynamic> args) async {
    SendPort resultPort = args[0];
    bool isConnect = false;

    final ip = args[1];
    print(ip);
    final ProcessResult  response  ;
    try {
      final result = await InternetAddress.lookup(ip);
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {

        // final response = await Process.run('ping', ['-c', '1', '-W', '5', ip]);

        if (Platform.isWindows) {
          response = await Process.run('ping', ['/n', '4', '/w', '5000', ip]);
        } else {
          response =  await Process.run('arp', ['-a']);
        }

        if (response.exitCode == 0) {
          isConnect =  true;

          print('isConnect $isConnect');
        }

        Isolate.exit(resultPort,response.stdout);
      }
      print('result 出错$result');
      isConnect =  false;
      // Isolate.exit(resultPort,response);
    } catch (e) {
      print('异常');
      isConnect =  false;
      // Isolate.exit(resultPort,response);
    }
  }

  Widget? getStartIpInput(){
    return GetInputTextFieldUtils.getInputTextField(
      TextInputType.number,
      obscureText:false,
      controller: _IpinpuController,
      decoration: InputDecoration(
        hintText: "请输入ip",
        labelText: "ip",
        icon: const Icon(
          Icons.computer,
          size: 20.0,
        ),
        border: const OutlineInputBorder(),
        //使用 GestureDetector 实现手势识别
        suffixIcon: GestureDetector(
          child: Offstage(
            child: Icon(Icons.clear),
            offstage: Ip == '',
          ),
          //点击清除文本框内容
          onTap: () {
            this.setState(() {
              Ip = '';
              _IpinpuController.clear();
            });
          },
        ),
      ),
      onChanged: (value) {
        this.setState(() {
          Ip = value;
        });
      },
    );
  }

}